(function($){

jQuery.fn.cells = function (options) {
    var settings = jQuery.extend({
        tableWidth: '100%',
        columns: 5,
        rows: 8,
        columnName: ['星期一', '星期二', '星期三', '星期四', '星期五'],
        columnWidth: ['16.66%', '16.66%', '16.66%', '16.66%', '16.66%', '16.66%'],
        rowName: ['第一節', '第二節', '第三節', '第四節', '第五節', '第六節', '第七節', '第八節'],
        hasColumnName: true,
        hasRowName: true,
        onCellClicked: function (event) {
            var reg = new RegExp(/ttcol_(\d+)_(\d+)_/);
            var result = reg.exec($(this).attr('id'));
            var row = result[1];
            var col = result[2];

            if (row == 0 || col == 0) {
                //do nothing
            } else {
                console.log('current cell => row:' + row + ',col:' + col);
            }
        },
        onEventClicked: function (event) {
            var isRemoved = confirm('刪除:' + $(this).attr('id') + '?');
            if(isRemoved){
            	removeEvent(newEvent.attr('id'));
            }
            event.stopPropagation();
        },
        data:[]
    }, options);
	
	/**
	 * add event on cell
	 */
    function addEvent(){
        var myID = this.attr('id');
        var datas = arguments;
        for(key in datas){
            var data = datas[key];
            appendEvent(data, myID);
        }
        
    }
    
    /**
     * append event on cell really
     */
    function appendEvent(data, myID){
    	var newEvent = $('<div id="e_' + data.row + '_' + data.column + '_' + (new Date).getTime() + '" class="c_event">' + data.title + '</div>');
    	newEvent.attr('desc', data.desc);
    	newEvent.on('click', settings.onEventClicked);
        $('#ttcol_' + data.row + '_' + data.column + '_' + myID).append(newEvent);
        
    }
    
    /**
     * remove event from cell by id
     */
    function removeEvent(id){
    	console.log('remove id:' + id);
    	//$('#' + id).remove();
    }
    
    
    $(this).data('addEvent', addEvent);
    $(this).data('removeEvent', removeEvent);
    
    if (typeof options == 'string') {
        var args = Array.prototype.slice.call(arguments, 1);
        //var res;
        this.each(function() {
            // $(this).data(options).apply('123', function(){alert('我是callback');});
            //$(this).data(options).apply('123', [function(){alert('我是callback');}]);
            $(this).data(options).apply($(this), args);
            /*
            var calendar = $.data(this, 'fullCalendar');
            if (calendar && $.isFunction(calendar[options])) {
                var r = calendar[options].apply(calendar, args);
                if (res === undefined) {
                    res = r;
                }
                if (options == 'destroy') {
                    $.removeData(this, 'fullCalendar');
                }
            }
            */
        });
        /*
        if (res !== undefined) {
            return res;
        }
        */
        return this;
    }




    return this.each(
        function () {
            $(this).empty().css('width', settings.tableWidth);
            var myID = $(this).attr('id');
            var startCol = settings.hasRowName ? 0 : 1;
            var startRow = settings.hasColumnName ? 0 : 1;
            var myTable = $('<table class="ttTable" id="tb_' + myID + '"></table>');
            myTable.css('width', settings.tableWidth);
            $(this).append(myTable);
            for (var row = startRow; row <= settings.rows; row++) {
                var newRow = $('<tr id="ttrow_' + row + '_' + myID + '"></tr>');
                for (var col = startCol; col <= settings.columns; col++) {
                    //var cell = $('<td class="ttcol" id="ttcol_' + row + '_' + col + '_' + myID + '" style="width:' + settings.columnWidth[col - startCol] + '">' + row + ',' + col + '</td>');
                    var cell = $('<td class="ttcol" id="ttcol_' + row + '_' + col + '_' + myID + '" style="width:' + settings.columnWidth[col - startCol] + '"></td>');
                    cell.on('click', settings.onCellClicked);
                    newRow.append(cell);
                }
                myTable.append(newRow);
            }
            //加上欄及列的標籤
            startCol = 1;
            startRow = 1;
            for (var col = startCol; col <= settings.columnName.length; col++) {
                $('#ttcol_0_' + col + '_' + myID).html(settings.columnName[col - startCol]).attr('class', 'ttTh');
            }
            for (var row = startRow; row <= settings.rowName.length; row++) {
                $('#ttcol_' + row + '_0_' + myID).html(settings.rowName[row - startRow]).attr('class', 'ttTh');
            }

            var myData = settings.data;
            if (myData.length > 0) {
                for (var key in myData) {
                    var data = myData[key];
                    //alert('row:' + data.row + ',col:' + data.column);
                    appendEvent(data, myID);
                }
            }
            
        });
};

})(jQuery);

